#version 120

uniform vec2 screensize;
uniform float beat_size_vec[28];
uniform float currentTime;

// python:
// for i in xrange(28):
//      color = colorsys.hsv_to_rgb(0.6-i*1.0/28, 0.95, 1.0)
//      print "vec4(%s, %s, %s, 1.0)," % color

const vec4 palette[28] = vec4[28](
vec4(0.05, 0.43, 1.0, 1.0),
vec4(0.05, 0.633571428571, 1.0, 1.0),
vec4(0.05, 0.837142857143, 1.0, 1.0),
vec4(0.05, 1.0, 0.959285714286, 1.0),
vec4(0.05, 1.0, 0.755714285714, 1.0),
vec4(0.05, 1.0, 0.552142857143, 1.0),
vec4(0.05, 1.0, 0.348571428571, 1.0),
vec4(0.05, 1.0, 0.145, 1.0),
vec4(0.158571428571, 1.0, 0.05, 1.0),
vec4(0.362142857143, 1.0, 0.05, 1.0),
vec4(0.565714285714, 1.0, 0.05, 1.0),
vec4(0.769285714286, 1.0, 0.05, 1.0),
vec4(0.972857142857, 1.0, 0.05, 1.0),
vec4(1.0, 0.823571428571, 0.05, 1.0),
vec4(1.0, 0.62, 0.05, 1.0),
vec4(1.0, 0.416428571429, 0.05, 1.0),
vec4(1.0, 0.212857142857, 0.05, 1.0),
vec4(1.0, 0.00928571428571, 0.05, 1.0),
vec4(1.0, -0.194285714286, 0.05, 1.0),
vec4(1.0, -0.397857142857, 0.05, 1.0),
vec4(1.0, -0.601428571429, 0.05, 1.0),
vec4(1.0, -0.805, 0.05, 1.0),
vec4(1.0, 0.05, 1.10857142857, 1.0),
vec4(1.0, 0.05, 1.31214285714, 1.0),
vec4(1.0, 0.05, 1.51571428571, 1.0),
vec4(1.0, 0.05, 1.71928571429, 1.0),
vec4(1.0, 0.05, 1.92285714286, 1.0),
vec4(-0.126428571429, 0.05, 1.0, 1.0));
	
// python
//import numpy as np
//from math import *
//angles = np.linspace(0.0, pi*6, num=28)
//translates = 1-np.logspace(0.0, -1.0, num=28)
//a = 0.2
//b = 0.7/(6*180)
//for angle,dist in zip(angles,translates):
//    rotatem = np.matrix( ((cos(angle), -sin(angle), 0.0),
//        (sin(angle), cos(angle), 0.0),
//        (0.0, 0.0, 1.0)) )
//    translatem = np.matrix( ((1.0, 0.0, 0.0),
//        (0.0, 1.0, 0.0),
//        (dist, 0, 1.0)))
//    pos = np.array((0.0, 0.0, 1.0))*translatem*rotatem*0.5
//    print "vec2(%s, %s)," % (pos[0,0], pos[0,1])



const vec2 spiral[28] = vec2[28](
    vec2(0.0, 0.0),
vec2(0.0313104259117, -0.0262725668345),
vec2(0.0136148046855, -0.0772133942906),
vec2(-0.0564340793297, -0.0977466926774),
vec2(-0.135799235919, -0.0494268797107),
vec2(-0.163106153345, 0.0593657848432),
vec2(-0.10012893742, 0.17342840692),
vec2(0.0390292529048, 0.221345892432),
vec2(0.189412270761, 0.158935766536),
vec2(0.267920558319, 6.56216108343e-17),
vec2(0.219772067227, -0.184410660553),
vec2(0.0528433492517, -0.299689525899),
vec2(-0.160154658405, -0.277396005426),
vec2(-0.314795393315, -0.114576153051),
vec2(-0.327470141615, 0.119189384159),
vec2(-0.180436014945, 0.3125243454),
vec2(0.0646396911584, 0.366589905292),
vec2(0.293156442989, 0.24598746319),
vec2(0.392278265498, 1.92160928887e-16),
vec2(0.307248212216, -0.25781186155),
vec2(0.0710516266934, -0.40295379872),
vec2(-0.20829748657, -0.360781829828),
vec2(-0.397878049858, -0.144815767016),
vec2(-0.403761146876, 0.146957039216),
vec2(-0.217711258375, 0.377086960885),
vec2(0.0765270035806, 0.434006204116),
vec2(0.341310222126, 0.286393281503),
vec2(0.45, 3.3065463577e-16));

	
void main(){

	vec4 color;
    float dist;
    float beat_size;
    float sina;
    float cosa;
    float angle;
    int i;

    color = vec4(0.0, 0.0, 0.0, 1.0);
    //get the relative fragment position
    float maxsize = max(screensize.x, screensize.y);
	vec2 fragpos = gl_FragCoord.xy/maxsize;
    vec2 addition = screensize/(2*maxsize);
    float distances[28];
    for(i = 0; i<28; i++){
        angle = currentTime*i/56.0;
        sina = sin(angle);
        cosa = cos(angle);
        vec2 rotatedspiral = mat2(cosa, sina, -sina, cosa)*spiral[i]+addition;
        distances[i] = pow(log(1+0.1/distance(fragpos, rotatedspiral)), 2);
    }
    
    for(i = 0; i<28; i++){
        beat_size = beat_size_vec[i];
        dist = distances[i];
        color.rgb += palette[i].rgb*dist*beat_size*(28-i)/28.0*2.0;
    }
    //color = vec4(0.3, 0.4, 0.5, 1.0);
     
	gl_FragColor = color;	
}
